<!--
 * @Description: 设置商品限购弹窗
 * @Author: liyujie
 * @Date: 2021/11/02
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/11/02
-->
<template>
    <section class="wrapper">
        <el-dialog
            title="限购设置"
            :visible.sync="dialogVisible"
            width="680px"
            :close-on-click-modal="false"
            @close="$emit('close')"
        >
            <div class="dialog-content">
                <el-form
                    :model="ruleForm"
                    :rules="rules"
                    ref="ruleForm"
                    label-width="98px"
                    @submit.native.prevent
                >
                    <el-form-item
                        label="限购数量："
                        prop="buy_limit_count"
                    >
                        <el-input-number
                            v-model="ruleForm.buy_limit_count"
                            size="medium"
                            placeholder="-1为不限制"
                            style="width: 380px"
                            :min="-1"
                            :controls="false"
                            :step="1"
                            :step-strictly="true"
                        />
                    </el-form-item>
                </el-form>
            </div>
            <div
                class="dialog-footer"
                style="padding-top: 12px"
            >
                <el-button
                    size="medium"
                    style="width: 120px"
                    @click="dialogVisible = false"
                >
                    取消
                </el-button>
                <el-button
                    type="primary"
                    size="medium"
                    style="width: 120px;margin-left: 32px"
                    @click="handleConfirm()"
                >
                    确定
                </el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
import {deepClone} from "@/assets/js/utils";
import Vue from "vue";
const numberReg = /(?:^[1-9]([0-9]+)?(?:\.[0-9]{1,2})?$)|(?:^(?:0)$)|(?:^[0-9]\.[0-9](?:[0-9])?$)/;

const Dialog = {
    name: "SetGoodsLimit",
    props: {
        default_value: {
            type: Number,
            default: -1
        }
    },
    data() {
        return {
            dialogVisible: false,
            ruleForm: {
                buy_limit_count: -1
            },
            rules: {
                buy_limit_count: [
                    { required: true, message: '请输入限购数量', trigger: 'blur' },
                ]
            }
        };
    },
    mounted() {
        this.ruleForm.buy_limit_count = this.default_value;
    },
    methods: {

        /**
         * 显示弹窗
         */
        show() {
            this.dialogVisible = true;
        },

        /**
         * 显示弹窗
         */
        close() {
            this.dialogVisible = false;
        },

        /**
         * 点击确定
         */
        handleConfirm() {
            this.$refs.ruleForm.validate((valid) => {
                if(valid) {
                    this.$emit('confirm', this.ruleForm.buy_limit_count);
                }
            });
        },
    }
};

export default Dialog;

const setGoodsLimitDialog = function (config) {
    return new Promise((resolve) => {
        const DialogConstructor = Vue.extend(Dialog);
        const instance = new DialogConstructor({
            propsData: config,
        }).$mount();

        instance.$on("close", function () {
            setTimeout(() => {
                document.body.removeChild(instance.$el);
                instance.$destroy();
            }, 300);
            resolve(false);
        });
        instance.$on("confirm", function (data) {
            instance.close();
            resolve(data);
        });
        document.body.appendChild(instance.$el);
        instance.dialogVisible = true;
    });
};

export { setGoodsLimitDialog };
</script>

<style scoped lang="less">
.wrapper {
    .dialog-content {
        display: flex;
        flex-direction: column;
        padding: 0 32px;
    }
    .dialog-footer {
        display: flex;
        justify-content: center;
    }
}

/deep/ .el-dialog__header {
    padding-top: 10px;
    box-shadow: 0 1px 0 0 rgba(233, 233, 233, 1);
    display: flex;
    align-items: center;
    background: #FAFAFA;
    border-radius: 4px 4px 0 0;

    .el-dialog__title {
        font-weight: 500;
        color: #333;
        font-size: 14px;
    }

    .el-icon-close:before {
        color: rgb(194, 194, 194);
        font-size: 17px;
        font-weight: bold;
    }

    .el-dialog__headerbtn {
        top: 14px;
    }
}

/deep/ .el-dialog__body {
    padding: 24px 32px;
}

/deep/ .el-textarea__inner {
    resize: none;
}
/deep/ .el-input-number .el-input__inner {
    text-align: left;
}
</style>
